
**********************************************************************************
		Marlene Mauk: Quality of democracy makes a difference, but not for everyone
		replication code
		2021-04-22
**********************************************************************************

cd [working directory]


*************************
     instructions
*************************

/* step 1
download all relevant data from their respective sources:
WVS 1981-2020: https://www.worldvaluessurvey.org/wvs.jsp
V-Dem v10: https://www.v-dem.net/en/
WDI: https://databank.worldbank.org
*/

/* step 2
replace [path] throughout this Stata and in the Mplus code
*/

/* step 3
run this Stata code, lines 51-449
*/

/* step 3
execute Mplus code and follow instructions therein
demqual.inp to import data to Mplus
model1.inp for Model 1: direct effect of democratic performance evaluations on trust
model2.inp for Model 2: direct effect of democratic quality on trust (controlling for democratic performance evaluations)
model3.inp for Model 3: indirect effect of democratic quality on trust (via democratic performance evaluations)
model4.inp for Model 4: interaction of democratic quality x political interest  democratic performance evaluations  trust
model5_sec.inp and model5_tert.inp for Model 5: interaction of democratic quality x education  democratic performance evaluations  trust
model6.inp for Model 6: interaction of democratic quality x conceptions of democracy  democratic performance evaluations  trust 
*/

/* step 4
run this Stata code, lines 468-512
*/



*************************
  preparing the data
*************************

//** WVS 1981-2020 **//
use "[path]\WVS_TimeSeries_stata_v1_2.dta"

numlabel _all, add

gen year = S020

gen polint = 1-((E023-1)/3)
gen soctrust = 1-(A165-1)
gen finsat = ((C006-1)/9)
gen trustparl = 1-((E069_07-1)/3)
gen trustgov = 1-((E069_11-1)/3)
gen trustcourt = 1-((E069_17-1)/3)
gen dpe = (E236-1)/9
gen female = X001-1
gen age = X003
gen education = .
replace education = 1 if X025==1 | X025==2 | X025R==1
replace education = 2 if X025==3 | X025==4 | X025==5 | X025==6 | X025R==2
replace education = 3 if X025==7 | X025==8 | X025R==3
replace X045B = 1 if X045==1
replace X045B = 2 if X045==2
replace X045B = 4 if X045==3
replace X045B = 5 if X045==4
replace X045B = 6 if X045==5
gen income = (X047-1)/9
recode E226 E229 E233 (0=1) /*conceptions of democracy have only been fielded from 2005 onwards*/
gen libcon1 = (E226-1)/9
gen libcon2 = (E229-1)/9
gen libcon3 = (E233-1)/9

rename COW_NUM cowcode
tab cowcode /*also has 345 Yugoslavia when it clearly didn't exist anymore; must have been Serbia-Montenegro until 2006 and Serbia after*/
replace cowcode=993 if cowcode==345 /*Serbia*/
replace cowcode=994 if cowcode==667 /*Palestine*/
replace cowcode=995 if cowcode==714 /*Hong Kong*/
replace cowcode=255 if cowcode==260 /*change cowcode for German Federal Republic to cowcode for Germany*/

keep year cowcode polint soctrust finsat trustparl trustgov trustcourt dpe age female education income libcon1 libcon2 libcon3

save wvs.dta, replace


//** V-Dem **//
use "[path]\V-Dem-CY-Full+Others-v10.dta"

drop if year <=1979
keep v2x_libdem COWcode year country_id country_name v2x_regime e_peaveduc
rename COWcode cowcode

tab country_name if cowcode==.
tab cowcode if country_name=="Serbia" /*Serbia has COWcode of Yugoslavia --> change*/
replace cowcode=993 if country_name=="Serbia"
replace cowcode=994 if country_name=="Palestine/West Bank"
replace cowcode=995 if country_name=="Hong Kong"

drop if cowcode==.
drop country_name
rename year vdemyear
gen year=vdemyear+1
save vdem_v10.dta, replace


//** WDI data **//
clear
import delimited using "[path]\wdi.csv"
rename ïcountryname country_name
rename time year
drop timecode countrycode
rename urbanpopulation urbanpop
rename gdppercapitaconstant2010 gdpcap2010
rename gdpgrowth gdpgrowth
rename lifeexp lifeexp

replace urbanpop="." if urbanpop==".."
replace gdpcap2010="." if gdpcap2010==".."
replace gdpgrowth="." if gdpgrowth==".."
replace lifeexp="." if lifeexp==".."
destring urbanpop gdpcap2010 gdpgrowth lifeexp, replace
sum urbanpop gdpcap2010 gdpgrowth lifeexp year

gen cowcode=.
replace cowcode=2 if country_name=="United States"
replace cowcode=20 if country_name=="Canada"
replace cowcode=31 if country_name=="Bahamas, The"
replace cowcode=40 if country_name=="Cuba"
replace cowcode=41 if country_name=="Haiti"
replace cowcode=42 if country_name=="Dominican Republic"
replace cowcode=51 if country_name=="Jamaica"
replace cowcode=52 if country_name=="Trinidad and Tobago"
replace cowcode=53 if country_name=="Barbados"
replace cowcode=54 if country_name=="Dominica"
replace cowcode=55 if country_name=="Grenada"
replace cowcode=56 if country_name=="St. Lucia"
replace cowcode=57 if country_name=="St. Vincent and the Grenadines"
replace cowcode=58 if country_name=="Antigua and Barbuda"
replace cowcode=60 if country_name=="St. Kitts and Nevis"
replace cowcode=70 if country_name=="Mexico"
replace cowcode=80 if country_name=="Belize"
replace cowcode=90 if country_name=="Guatemala"
replace cowcode=91 if country_name=="Honduras"
replace cowcode=92 if country_name=="El Salvador"
replace cowcode=93 if country_name=="Nicaragua"
replace cowcode=94 if country_name=="Costa Rica"
replace cowcode=95 if country_name=="Panama"
replace cowcode=100 if country_name=="Colombia"
replace cowcode=101 if country_name=="Venezuela, RB"
replace cowcode=110 if country_name=="Guyana"
replace cowcode=115 if country_name=="Suriname"
replace cowcode=130 if country_name=="Ecuador"
replace cowcode=135 if country_name=="Peru"
replace cowcode=140 if country_name=="Brazil"
replace cowcode=145 if country_name=="Bolivia"
replace cowcode=150 if country_name=="Paraguay"
replace cowcode=155 if country_name=="Chile"
replace cowcode=160 if country_name=="Argentina"
replace cowcode=165 if country_name=="Uruguay"
replace cowcode=200 if country_name=="United Kingdom"
replace cowcode=205 if country_name=="Ireland"
replace cowcode=210 if country_name=="Netherlands"
replace cowcode=211 if country_name=="Belgium"
replace cowcode=212 if country_name=="Luxembourg"
replace cowcode=220 if country_name=="France"
replace cowcode=221 if country_name=="Monaco"
replace cowcode=223 if country_name=="Liechtenstein"
replace cowcode=225 if country_name=="Switzerland"
replace cowcode=230 if country_name=="Spain"
replace cowcode=232 if country_name=="Andorra"
replace cowcode=235 if country_name=="Portugal"
replace cowcode=255 if country_name=="Germany"
replace cowcode=290 if country_name=="Poland"
replace cowcode=305 if country_name=="Austria"
replace cowcode=310 if country_name=="Hungary"
replace cowcode=315 if country_name=="Czechoslovakia"
replace cowcode=316 if country_name=="Czech Republic"
replace cowcode=317 if country_name=="Slovak Republic"
replace cowcode=325 if country_name=="Italy"
replace cowcode=331 if country_name=="San Marino"
replace cowcode=338 if country_name=="Malta"
replace cowcode=339 if country_name=="Albania"
replace cowcode=341 if country_name=="Montenegro"
replace cowcode=343 if country_name=="North Macedonia"
replace cowcode=344 if country_name=="Croatia"
replace cowcode=345 if country_name=="Yugoslavia"
replace cowcode=346 if country_name=="Bosnia and Herzegovina"
replace cowcode=347 if country_name=="Kosovo"
replace cowcode=349 if country_name=="Slovenia"
replace cowcode=350 if country_name=="Greece"
replace cowcode=352 if country_name=="Cyprus"
replace cowcode=355 if country_name=="Bulgaria"
replace cowcode=359 if country_name=="Moldova"
replace cowcode=360 if country_name=="Romania"
replace cowcode=365 if country_name=="Russian Federation"
replace cowcode=366 if country_name=="Estonia"
replace cowcode=367 if country_name=="Latvia"
replace cowcode=368 if country_name=="Lithuania"
replace cowcode=369 if country_name=="Ukraine"
replace cowcode=370 if country_name=="Belarus"
replace cowcode=371 if country_name=="Armenia"
replace cowcode=372 if country_name=="Georgia"
replace cowcode=373 if country_name=="Azerbaijan"
replace cowcode=375 if country_name=="Finland"
replace cowcode=380 if country_name=="Sweden"
replace cowcode=385 if country_name=="Norway"
replace cowcode=390 if country_name=="Denmark"
replace cowcode=395 if country_name=="Iceland"
replace cowcode=402 if country_name=="Cabo Verde"
replace cowcode=403 if country_name=="Sao Tome and Principe"
replace cowcode=404 if country_name=="Guinea-Bissau"
replace cowcode=411 if country_name=="Equatorial Guinea"
replace cowcode=420 if country_name=="Gambia, The"
replace cowcode=432 if country_name=="Mali"
replace cowcode=433 if country_name=="Senegal"
replace cowcode=434 if country_name=="Benin"
replace cowcode=435 if country_name=="Mauritania"
replace cowcode=436 if country_name=="Niger"
replace cowcode=437 if country_name=="Cote d'Ivoire"
replace cowcode=438 if country_name=="Guinea"
replace cowcode=439 if country_name=="Burkina Faso"
replace cowcode=450 if country_name=="Liberia"
replace cowcode=451 if country_name=="Sierra Leone"
replace cowcode=452 if country_name=="Ghana"
replace cowcode=461 if country_name=="Togo"
replace cowcode=471 if country_name=="Cameroon"
replace cowcode=475 if country_name=="Nigeria"
replace cowcode=481 if country_name=="Gabon"
replace cowcode=482 if country_name=="Central African Republic"
replace cowcode=483 if country_name=="Chad"
replace cowcode=484 if country_name=="Congo, Rep."
replace cowcode=490 if country_name=="Congo, Dem. Rep."
replace cowcode=500 if country_name=="Uganda"
replace cowcode=501 if country_name=="Kenya"
replace cowcode=510 if country_name=="Tanzania"
replace cowcode=516 if country_name=="Burundi"
replace cowcode=517 if country_name=="Rwanda"
replace cowcode=520 if country_name=="Somalia"
replace cowcode=522 if country_name=="Djibouti"
replace cowcode=530 if country_name=="Ethiopia"
replace cowcode=531 if country_name=="Eritrea"
replace cowcode=540 if country_name=="Angola"
replace cowcode=541 if country_name=="Mozambique"
replace cowcode=551 if country_name=="Zambia"
replace cowcode=552 if country_name=="Zimbabwe"
replace cowcode=553 if country_name=="Malawi"
replace cowcode=560 if country_name=="South Africa"
replace cowcode=565 if country_name=="Namibia"
replace cowcode=570 if country_name=="Lesotho"
replace cowcode=571 if country_name=="Botswana"
replace cowcode=572 if country_name=="Eswatini"
replace cowcode=580 if country_name=="Madagascar"
replace cowcode=581 if country_name=="Comoros"
replace cowcode=590 if country_name=="Mauritius"
replace cowcode=591 if country_name=="Seychelles"
replace cowcode=600 if country_name=="Morocco"
replace cowcode=615 if country_name=="Algeria"
replace cowcode=616 if country_name=="Tunisia"
replace cowcode=620 if country_name=="Libya"
replace cowcode=625 if country_name=="Sudan"
replace cowcode=626 if country_name=="South Sudan"
replace cowcode=630 if country_name=="Iran, Islamic Rep."
replace cowcode=640 if country_name=="Turkey"
replace cowcode=645 if country_name=="Iraq"
replace cowcode=651 if country_name=="Egypt, Arab Rep."
replace cowcode=652 if country_name=="Syrian Arab Republic"
replace cowcode=660 if country_name=="Lebanon"
replace cowcode=663 if country_name=="Jordan"
replace cowcode=666 if country_name=="Israel"
replace cowcode=670 if country_name=="Saudi Arabia"
replace cowcode=679 if country_name=="Yemen, Rep."
replace cowcode=690 if country_name=="Kuwait"
replace cowcode=692 if country_name=="Bahrain"
replace cowcode=694 if country_name=="Qatar"
replace cowcode=696 if country_name=="United Arab Emirates"
replace cowcode=698 if country_name=="Oman"
replace cowcode=700 if country_name=="Afghanistan"
replace cowcode=701 if country_name=="Turkmenistan"
replace cowcode=702 if country_name=="Tajikistan"
replace cowcode=703 if country_name=="Kyrgyz Republic"
replace cowcode=704 if country_name=="Uzbekistan"
replace cowcode=705 if country_name=="Kazakhstan"
replace cowcode=710 if country_name=="China"
replace cowcode=712 if country_name=="Mongolia"
replace cowcode=713 if country_name=="Taiwan"
replace cowcode=731 if country_name=="Korea, Dem. Peopleâs Rep."
replace cowcode=732 if country_name=="Korea, Rep."
replace cowcode=740 if country_name=="Japan"
replace cowcode=750 if country_name=="India"
replace cowcode=760 if country_name=="Bhutan"
replace cowcode=770 if country_name=="Pakistan"
replace cowcode=771 if country_name=="Bangladesh"
replace cowcode=775 if country_name=="Myanmar"
replace cowcode=780 if country_name=="Sri Lanka"
replace cowcode=781 if country_name=="Maldives"
replace cowcode=790 if country_name=="Nepal"
replace cowcode=800 if country_name=="Thailand"
replace cowcode=811 if country_name=="Cambodia"
replace cowcode=812 if country_name=="Lao PDR"
replace cowcode=816 if country_name=="Vietnam"
replace cowcode=820 if country_name=="Malaysia"
replace cowcode=830 if country_name=="Singapore"
replace cowcode=835 if country_name=="Brunei Darussalam"
replace cowcode=840 if country_name=="Philippines"
replace cowcode=850 if country_name=="Indonesia"
replace cowcode=860 if country_name=="Timor-Leste"
replace cowcode=900 if country_name=="Australia"
replace cowcode=910 if country_name=="Papua New Guinea"
replace cowcode=920 if country_name=="New Zealand"
replace cowcode=935 if country_name=="Vanuatu"
replace cowcode=940 if country_name=="Solomon Islands"
replace cowcode=946 if country_name=="Kiribati"
replace cowcode=947 if country_name=="Tuvalu"
replace cowcode=950 if country_name=="Fiji"
replace cowcode=955 if country_name=="Tonga"
replace cowcode=970 if country_name=="Nauru"
replace cowcode=983 if country_name=="Marshall Islands"
replace cowcode=986 if country_name=="Palau"
replace cowcode=987 if country_name=="Micronesia, Fed. Sts."
replace cowcode=990 if country_name=="Samoa"
replace cowcode=993 if country_name=="Serbia"
replace cowcode=994 if country_name=="West Bank and Gaza"
replace cowcode=995 if country_name=="Hong Kong SAR, China"

tab country_name if cowcode==.
drop if cowcode==.
rename year wdiyear
gen year=wdiyear+1
save wdi.dta, replace


//** merging the data **//
use wvs.dta
merge m:1 cowcode year using wdi.dta, keepusing(urbanpop gdpcap2010 gdpgrowth lifeexp wdiyear)
tab cowcode year if _merge==1 /*that's countries we don't have WDI data for*/
tab cowcode year if _merge==2 /*that's country-years we don't have survey data for*/
drop if _merge==1
drop if _merge==2
drop _merge
save wvs.dta, replace

merge m:1 cowcode year using vdem_v10.dta, keepusing(v2x_libdem v2x_regime e_peaveduc vdemyear)
tab cowcode year if _merge==1 /*that's country-years we don't have v-dem data for*/
tab cowcode year if _merge==2 /*that's country-years we don't have survey data for*/
drop if _merge==1
drop if _merge==2
drop _merge
label define COW_NUM 994 "Palestine" 995 "Hong Kong", modify
numlabel COW_NUM, add force
drop if year<2005 /*dpe only included 2005+ --> remove everything older than that*/
gen gdplog = log(gdpcap2010)
drop gdpcap2010
gen democratic = .
replace democratic = 0 if v2x_regime==0 | v2x_regime==1
replace democratic = 1 if v2x_regime==2 | v2x_regime==3
tab democratic v2x_regime

gen yearnew = year-2000
replace cowcode=102 if cowcode==2 /*recoding those so i can easily construct a country-year variable*/
replace cowcode=120 if cowcode==20
replace cowcode=141 if cowcode==41
replace cowcode=152 if cowcode==52
replace cowcode=170 if cowcode==70
replace cowcode=190 if cowcode==90
replace cowcode=193 if cowcode==93
label define COW_NUM 102 "United States" 120 "Canada" 141 "Haiti" 152 "Trinidad and Tobago" 170 "Mexico" 190 "Guatemala" 193 "Nicaragua", modify
numlabel COW_NUM, add force

gen coyear = cowcode*100 + yearnew
drop yearnew

tab education
gen primary = .
replace primary = 1 if education==1
replace primary = 0 if education==2 | education==3
gen secondary = .
replace secondary = 1 if education==2
replace secondary = 0 if education==1 | education==3
gen tertiary = .
replace tertiary = 1 if education==3
replace tertiary = 0 if education==1 | education==2

gen y2005=0
replace y2005=1 if year==2005
gen y2006=0
replace y2006=1 if year==2006
gen y2007=0
replace y2007=1 if year==2007
gen y2008=0
replace y2008=1 if year==2008
gen y2009=0
replace y2009=1 if year==2009
gen y2010=0
replace y2010=1 if year==2010
gen y2011=0
replace y2011=1 if year==2011
gen y2012=0
replace y2012=1 if year==2012
gen y2013=0
replace y2013=1 if year==2013
gen y2014=0
replace y2014=1 if year==2014
gen y2015=0
replace y2015=1 if year==2015
gen y2016=0
replace y2016=1 if year==2016
gen y2017=0
replace y2017=1 if year==2017
gen y2018=0
replace y2018=1 if year==2018
gen y2019=0
replace y2019=1 if year==2019
gen y2020=0
replace y2020=1 if year==2020

save wvs.dta, replace



*************************
		analyses
*************************
use wvs.dta

* CFA for liberal conception of democracy
sem (libcon_ -> libcon1) (libcon_ -> libcon2) (libcon_ -> libcon3), method(adf) iterate(50) latent(libcon_) nocapslatent
sem, standardized
estat gof, stats(all)
predict libcon, latent(libcon_)
replace libcon=. if (libcon1==. & libcon2==. & libcon3==.)
sum libcon
gen libcon01 = ((libcon-r(min))/(r(max)-r(min)))
drop libcon
rename libcon01 libcon

* exporting data to Mplus
stata2mplus coyear cowcode year trustparl trustgov trustcourt dpe libcon ///
polint soctrust finsat dpe female age income primary secondary tertiary ///
urbanpop gdplog gdpgrowth lifeexp democratic v2x_libdem e_peaveduc ///
y2005 y2006 y2007 y2008 y2009 y2010 y2011 y2012 y2013 y2014 y2016 y2017 y2018 y2019 y2020 ///
using "[path]\demqual", missing(999) replace


//** run the analyses in mplus **//

/*
demqual.inp to import data to Mplus
model1.inp for Model 1: direct effect of democratic performance evaluations on trust
model2.inp for Model 2: direct effect of democratic quality on trust (controlling for democratic performance evaluations)
model3.inp for Model 3: indirect effect of democratic quality on trust (via democratic performance evaluations)
model4.inp for Model 4: interaction of democratic quality x political interest  democratic performance evaluations  trust
model5_sec.inp and model5_tert.inp for Model 5: interaction of democratic quality x education  democratic performance evaluations  trust
model6.inp for Model 6: interaction of democratic quality x conceptions of democracy  democratic performance evaluations  trust 
*/



//** import plot data from Mplus and plot interaction effect for full indirect effect to produce panels in Figure 3 **//

clear all
import delimited xaxis demqual demqual_low demqual_high using "[path]\model4.dat", delimiter(tab, collapse) varnames(nonames) 
destring xaxis demqual demqual_low demqual_high, force replace
twoway ///
(rcap demqual_high demqual_low xaxis, color(gs1)) ///
(scatter demqual xaxis, color(gs1)),  /// 
graphregion(color(white)) legend(region(lp(blank))) ///
ytitle("effect of democratic quality on political trust") yline(0) yscale(range(0 0.3)) ///
xtitle("political interest") xlabel(0 "none" 0.33 "little" 0.66 "moderate" 0.99 "high") /// 
legend(off)
graph export fig3_model4.png, replace

clear all 
import delimited xaxis demqual demqual_low demqual_high using "[path]\model5_sec.dat", delimiter(tab, collapse) varnames(nonames) 
destring xaxis demqual demqual_low demqual_high, force replace
save model5_2level_sec_trust.dta, replace
clear
import delimited xaxis demqual demqual_low demqual_high using "[path]\model5_tert.dat", delimiter(tab, collapse) varnames(nonames)
destring xaxis demqual demqual_low demqual_high, force replace
gen xaxis2=xaxis+1
drop xaxis
rename xaxis2 xaxis
drop if xaxis==1
append using model5_2level_sec_trust.dta
twoway ///
(rcap demqual_high demqual_low xaxis, color(gs1)) ///
(scatter demqual xaxis, color(gs1)),  /// 
graphregion(color(white)) legend(region(lp(blank))) ///
ytitle("indirect effect of democratic quality on political trust") yline(0) ///
xtitle("level of education") xlabel(0 "primary" 1 "secondary" 2 "tertiary") /// 
legend(off)
graph export fig3_model5.png, replace

clear all 
import delimited xaxis demqual demqual_low demqual_high using "[path]\model6.dat", delimiter(tab, collapse) varnames(nonames) 
destring xaxis demqual demqual_low demqual_high, force replace
twoway ///
(line demqual xaxis, lp(solid) lc(gs1)) ///
(line demqual_low xaxis, lp(dash) lc(gs6)) ///
(line demqual_high xaxis, lp(dash) lc(gs6)), ///
graphregion(color(white)) legend(region(lp(blank))) ///
ytitle("indirect effect of democratic quality on political trust") yline(0) ///
xtitle("liberal conception of democracy") xlabel(0(0.1)1) ///
legend(off)
graph export fig3_model6.png, replace